<?php

$str = '/user/{name}/{id:\d<1,10>}';

$param=[];
/**
 * @param string $route 路由
 * @param $param 路由动态参数
 * @param bool $complete 完整匹配 后面加$
 * @return string|string[]
 */
function parse($route, &$param, $complete=true){
    //判断是否是动态路由
    if(strpos($route,'{') === false){
        return $route;
    }
    $isMatched = preg_match_all('/{[^}]+}/', $route, $matches);
    if($isMatched){
        $replace = [];
        foreach ($matches[0] as $match){
            $keyArr = explode(':',substr($match,1,-1));
            $param[] = $keyArr[0];
            $reg = '[^/]';
            if(isset($keyArr[1])){ //自定义的
                $reg = str_replace(['<','>'],['{','}'],$keyArr[1]);
            }
            $endStr = str_split ($reg);
            if(!in_array(end($endStr),['?','+','*','}'])){
                $reg .='+';
            }
            $replace[] = '('.$reg.')';
        }
       return str_replace($matches[0],$replace,$route).($complete?'$':'');
    }
    return $route;
}

//print_r(parse($str,$param));
//print_r($param);
function microtime_float()
{
    list($usec, $sec) = explode(" ", microtime());
    return ((float)$usec + (float)$sec);

}

function stringify($url){



}



//$start=microtime_float();
//for ($i=0;$i<1000;$i++){
    $reg = '^/user/([^\/]+)/(\d{1,10})$';
    $isDisplay = preg_match_all('#'.$reg.'#', '/user/test/12342', $matches);
var_dump($isDisplay,$matches);
if($isDisplay){
    array_shift( $matches);
}
var_dump($isDisplay,$matches);
//    echo 1;
//}
//$end = microtime_float();
//echo(PHP_EOL." t i m e :" .  round( $end - $start ,2) .PHP_EOL);


//
//$dispatcher = FastRoute\simpleDispatcher(function(FastRoute\RouteCollector $r) {
//    $r->addRoute('GET', '/users', 'get_all_users_handler');
//    // {id} must be a number (\d+)
//    $r->addRoute('GET', '/user/{id:\d+}', 'get_user_handler');
//    // The /{title} suffix is optional
//    $r->addRoute('GET', '/articles/{id:\d+}[/{title}]', 'get_article_handler');
//});